package cn.devzyh.oms.coms.express.controller;

import cn.devzyh.oms.common.core.util.R;
import cn.devzyh.oms.common.excel.annotation.ResponseExcel;
import cn.devzyh.oms.common.log.annotation.SysLog;
import cn.devzyh.oms.common.mybatis.domain.PageQuery;
import cn.devzyh.oms.coms.api.express.entity.Express;
import cn.devzyh.oms.coms.express.service.ExpressService;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 快递配置表
 *
 * @author devzyh
 * @date 2024-03-06
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/express")
public class ExpressController {

	private final ExpressService expressService;

	/**
	 * 分页查询
	 *
	 * @param page    分页对象
	 * @param express 查询条件
	 * @return R
	 */
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('coms_express_view')")
	public R getExpressPage(@ModelAttribute PageQuery<Express> page, @ModelAttribute Express express) {
		LambdaQueryWrapper<Express> wrapper = Wrappers.lambdaQuery();
		return R.ok(expressService.page(page.mybatisPlusPage(), wrapper));
	}

	/**
	 * 通过ID查询
	 *
	 * @param id id
	 * @return R
	 */
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('coms_express_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(expressService.getById(id));
	}

	/**
	 * 新增数据
	 *
	 * @param express 快递配置表
	 * @return R
	 */
	@SysLog("新增快递配置表")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('coms_express_add')")
	public R save(@RequestBody Express express) {
		return R.ok(expressService.save(express));
	}

	/**
	 * 修改数据
	 *
	 * @param express 快递配置表
	 * @return R
	 */
	@SysLog("修改快递配置表")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('coms_express_edit')")
	public R updateById(@RequestBody Express express) {
		return R.ok(expressService.updateById(express));
	}

	/**
	 * 通过ID删除
	 *
	 * @param ids id列表
	 * @return R
	 */
	@SysLog("通过ID删除快递配置表")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('coms_express_del')")
	public R removeById(@RequestBody Long[] ids) {
		return R.ok(expressService.removeBatchByIds(CollUtil.toList(ids)));
	}

	/**
	 * 导出Excel表格
	 *
	 * @param express 查询条件
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('coms_express_export')")
	public List<Express> export(@ModelAttribute Express express) {
		LambdaQueryWrapper<Express> wrapper = Wrappers.lambdaQuery();
		return expressService.list(wrapper);
	}

}